package lablayer.view.additional.toolpanelstates;

import javafx.scene.Node;
import javafx.scene.layout.Panel;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import lablayer.view.state.StateInterface;
import lablayer.view.state.StateFactory;

/**
 * @author Darkness
 */

public class ViewToolPanel extends ViewTool {
    public var width: Float;
    public var height: Float;
    var mainPanel: Panel;
    var tools: ViewTool[];
    var currentTool: Integer;

    init {
        this.currentTool = -1;
        this.id = ViewTool.PanelId;
    }

    override public function create(): Node {
        this.mainPanel = Panel {
            width: bind this.width
            height: bind this.height
            content: [
                Rectangle {
                   width: bind this.width
                   height: bind this.height
                   fill: Color.TRANSPARENT
                }
            ]
        }
    }

    public function addTool(tool: ViewTool): Void {
        if (tool != null) {
            insert tool into this.tools;
        }
    }

    public function nextTool(): Void {
        if (sizeof(this.tools) != 0) {
            if (this.currentTool != -1) {
                delete this.tools[this.currentTool] from this.mainPanel.content;
            }
            this.currentTool++;
            if (sizeof(this.tools) == this.currentTool) {
                this.currentTool = 0;
            }
            insert this.tools[this.currentTool] into this.mainPanel.content;

            //change interface state
            if (this.tools[this.currentTool].getId() == ViewTool.ComponentId) {
                StateFactory.getState().setStateId(StateInterface.COMPONENT_STATE);
            }
            if (this.tools[this.currentTool].getId() == ViewTool.LinkId) {
                StateFactory.getState().setStateId(StateInterface.LINK_STATE);
            }
        }
    }

    public function previousTool(): Void {
        if (sizeof(this.tools) != 0) {
            if (this.currentTool != -1) {
                delete this.tools[this.currentTool] from this.mainPanel.content;
            }
            this.currentTool--;
            if (this.currentTool == -1) {
                this.currentTool = (sizeof(this.tools) - 1);
            }
            insert this.tools[this.currentTool] into this.mainPanel.content;

            //change interface state
            if (this.tools[this.currentTool].getId() == ViewTool.ComponentId) {
                StateFactory.getState().setStateId(StateInterface.COMPONENT_STATE);
            }
            if (this.tools[this.currentTool].getId() == ViewTool.LinkId) {
                StateFactory.getState().setStateId(StateInterface.LINK_STATE);
            }
        }
    }

    public function getCurrentTool(): Integer {
        return this.currentTool;
    }
}
